package org.MobileSales.DataManagement;

import org.MobileSales.Model.Params.FieldType;
import org.MobileSales.Model.Params.Param;
import org.MobileSales.Model.Params.ParamType;
import org.MobileSales.Model.Products.Tax;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ParamsManager {
	public Context context;
	
	public ParamsManager(Context c)
	{
		this.context = c;
	}
	
	public Param GetParam(int paramId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, ParamTypeId, Name, Description, ReadOnly, Required, FieldType, DefaultValue, MaxLength, DisplayName" 
							+ " FROM Params" 
							+ " WHERE _id = " + paramId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Param result = new Param();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.ParamType = GetParamType(c.getInt(c.getColumnIndex("ParamTypeId")));
     		result.Name = c.getString(c.getColumnIndex("Name"));
     		result.Description = c.getString(c.getColumnIndex("Description"));
     		result.IsReadOnly = c.getInt(c.getColumnIndex("ReadOnly")) == 1 ? true : false;
     		result.IsRequired = c.getInt(c.getColumnIndex("Required")) == 1 ? true : false;   		
     		result.FieldType = FieldType.valueOf(c.getString(c.getColumnIndex("FieldType")));
     		result.DefaultValue = c.getString(c.getColumnIndex("DefaultValue"));
     		result.MaxLength = c.getInt(c.getColumnIndex("MaxLength"));
     		result.DisplayName = c.getString(c.getColumnIndex("DisplayName"));
     		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void StoreParam(Param param)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
				
			cv.put("Name",param.Name);
			cv.put("Description", param.Description);
			cv.put("ReadOnly", param.IsReadOnly);
			cv.put("Required", param.IsRequired);
			cv.put("FieldType", param.FieldType.name());
			cv.put("DefaultValue", param.DefaultValue);
			cv.put("MaxLength", param.MaxLength);
			cv.put("DisplayName", param.DisplayName);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (param.Id != 0)
				db.update("Params", cv, "_id ="+param.Id, null);
			else
				db.insert("Params", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveParam(int paramId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("Params", "_id ="+paramId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public ParamType GetParamType(int paramTypeId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT Name" 
							+ " FROM ParamTypes" 
							+ " WHERE _id = " + paramTypeId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		ParamType result = null;
     		result = ParamType.valueOf(c.getString(c.getColumnIndex("Name")));
     		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
/*	public void StoreParamType(ParamType paramType)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
				
			cv.put("Name",paramType.name());
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (tax.Id != 0)
				db.update("Taxes", cv, "_id ="+tax.Id, null);
			else
				db.insert("Taxes", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}
     	finally {
     		dbHelper.close();
     	}
	}*/
	
	public void RemoveParamType(int paramTypeId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("ParamTypes", "_id ="+paramTypeId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}
     	finally {
     		dbHelper.close();
     	}
	}
}
